package chapter08

/**
 * Created with IntelliJ IDEA.
 * ClassName: Test02_MatchTypes
 * Package: chapter08
 * Description:
 * User: fzykd
 *
 * @Author: LQH
 *          Date: 2023-06-29
 *          Time: 20:57
 */

object Test02_MatchTypes {

  //模式匹配类型
  def main(args: Array[String]): Unit = {
    //1.匹配常量
    def desConst(x: Any): String = x match {
      case 1 => "Num one"
      case "hello" => "String hello"
      case true => "Boolean true"
      case '+' => "Char +"
      //有限个数匹配就搞一个默认值 否则报错
      //下划线 只是一个占位符 也可以使用随便一个变量名称
      case _ => ""
    }

    println(desConst("hello"))
    println(desConst(0.3))

    println("---------------------")

    //匹配类型
    def desType(x: Any): String = x match {
      //定义的变量接收x的值
      case i: Int => "Int " + i
      case i: Char => "Char " + i
      case i: String => "String " + i
      case list: List[String] => "List " + list
      case arr: Array[Int] => "Array " + arr.mkString(",")
      case a => "其他类型 " + a
    }

    println(desType(99))
    println(desType('a'))
    println(desType(true))
    println(desType(List("hel", "asd"))) //List List(hel, asd)
    //List[Int]没有确切的判断出来 底层JVM存在泛型擦除 只能判断到List集合类型 泛型是被擦掉了
    println(desType(List(1, 2, 3))) //List List(1, 2, 3)
    //Array是不存在泛型擦除的 因为Array没有在Scala集合当中 是通过隐式转换之后的操作
    println(desType(Array("hell"))) //其他类型 [Ljava.lang.String;@75bd9247
    println(desType(Array(1, 2, 4))) //Array 1,2,4

    println("---------------------")
    //匹配数组
    for (arr <- List(
      Array(0),
      Array(1, 0),
      Array(0, 1, 0),
      Array(1, 1, 0),
      Array(2, 3, 7, 15),
      Array("hello", 1, 30),
    )) {
      val ret = arr match {
        case Array(0) => "0"
        case Array(1, 0) => "Array(1,0)"
        //指定匹配两个元素的数组
        case Array(x, y) => "Array: " + x + "," + y
        case Array(0, _*) => "以0开头的数组"
        case Array(x, 1, z) => "中间为1的三个元素的数组"
        case _ => "something else"
      }

      println(ret)

    }

    //匹配列表
    println("---------------")
    for (list <- List(
      List(0),
      List(1, 0),
      List(0, 0, 0),
      List(1, 1, 0),
      List(88)
    )) {
      val ret = list match {
        case List(0) => "0"
        case List(x, y) => "List" + x + "," + y
        case List(0, _*) => "0开头..."
        //匹配只有一个元素的list
        case List(a) => "List " + a
        case _ => "something else"
      }
      println(ret)
    }

    //方式二
    val list = List(1, 2, 5, 7, 24)
    list match {
      //双冒号 之前用于追加 运算是从右到左 前面两个是单个数 最后是一个列表 从左往右追加
      case first :: second :: rest => println(s"first: $first , second: $second ,rest: $rest")
      //first: 1 , second: 2 ,rest: List(5, 7, 24)
      case _ => println("something else")
      //如果连三个元素都没有 那就会执行这个
    }

    println("--------------------")
    //匹配元组

    for (tp <- List(
      //元组
      (0, 1),
      (0, 0),
      (0, 1, 0),
      (0, 1, 1),
      (1, 1, 56),
      ("hello", true, 0.5)
    )) {
      val res = tp match {
        case (a, b) => "" + a + "," + b
        //第一个元素是0的二元组
        case (0, _) => "(0,_)"
        case (a,1,_) => "(a,1,_)" + a
        case _ => "something eles"
      }
      println(res)
    }


  }
}
